package com.gitee.zhanyao.cloud.mqtest.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

@Configuration
@RemoteApplicationEventScan(basePackages = "com.gitee.zhanyao.cloud.mqtest")
public class RocketMqConfig {

    @Value("${server.port}")
    private int localServerPort;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * Listener on the {@link UserRemoteApplicationEvent}.
     * @param event {@link UserRemoteApplicationEvent}
     */
    @EventListener
    public void onEvent(UserRemoteApplicationEvent event) {
        System.out.printf("Server [port : %d] listeners on %s\n", localServerPort,
                event.getUser());
    }

    @EventListener
    public void onAckEvent(AckRemoteApplicationEvent event)
            throws JsonProcessingException {
        System.out.printf("Server [port : %d] listeners on %s\n", localServerPort,
                objectMapper.writeValueAsString(event));
    }

}
